package com.itheima.examination4;

import java.text.ParseException;
import java.util.Date;

public class DateUtilsTest {
    public static void main(String[] args) {
        System.out.println("=== DateUtils工具类测试 ===");

        // 测试Date转换为String
        System.out.println("1. 测试dateToString方法：");
        Date currentDate = new Date();
        String dateString = DateUtils.dateToString(currentDate);
        System.out.println("当前时间Date对象: " + currentDate);
        System.out.println("转换为字符串: " + dateString);

        // 测试String转换为Date
        System.out.println("\n2. 测试stringToDate方法：");
        String testString = "2024-12-25 15:30:45";
        try {
            Date parsedDate = DateUtils.stringToDate(testString);
            System.out.println("字符串: " + testString);
            System.out.println("转换为Date对象: " + parsedDate);

            // 验证转换是否正确
            String backToString = DateUtils.dateToString(parsedDate);
            System.out.println("再转换回字符串: " + backToString);
            System.out.println("转换是否一致: " + testString.equals(backToString));
        } catch (ParseException e) {
            System.out.println("日期解析错误: " + e.getMessage());
        }

        // 测试边界情况
        System.out.println("\n3. 测试边界情况：");

        // 测试null值
        System.out.println("null Date转字符串: " + DateUtils.dateToString(null));

        try {
            System.out.println("null字符串转Date: " + DateUtils.stringToDate(null));
        } catch (ParseException e) {
            System.out.println("null字符串解析异常: " + e.getMessage());
        }

        // 测试空字符串
        try {
            System.out.println("空字符串转Date: " + DateUtils.stringToDate(""));
        } catch (ParseException e) {
            System.out.println("空字符串解析异常: " + e.getMessage());
        }

        // 测试错误格式字符串
        try {
            DateUtils.stringToDate("2024/12/25 15:30:45");
        } catch (ParseException e) {
            System.out.println("错误格式字符串解析异常: " + e.getMessage());
        }
    }
}
